package com.ask.forMe.langchain4j.service;


import com.ask.forMe.model.dto.CustomerServiceContext;
import com.ask.forMe.model.dto.OrderInfoDTO;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

@Service
public class ContextService {
    /**
     * 从LLM输出中提取用户情绪分数
     *
     * @param output
     * @return
     */
    public int extractEmotionScore(String output) {
        try {
            JSONObject jsonObject = new JSONObject(output);
            return jsonObject.optInt("emotion_score"); // 如果字段不存在，返回 0
        } catch (JSONException e) {
            return 0; // JSON 格式错误时返回默认值
        }
    }

    /**
     * 提取对话上下文，方便人工客服处理
     *
     * @param output
     * @param userId
     */
    public CustomerServiceContext extractContext(String output, String userId) {
        JSONObject jsonObject = new JSONObject(output);
        CustomerServiceContext context = CustomerServiceContext.builder()
                .customerId(userId)
                .emotionScore(jsonObject.optInt("emotion_score"))
                .customerMood(jsonObject.optString("customer_mood"))
                .urgencyLevel(jsonObject.optInt("urgency_level"))
                .repeatCount(jsonObject.optInt("repeat"))
                .issueType(jsonObject.optString("issue_type"))
                .build();
        return context;
    }
}
